do "E:\yungyu\lottery_baby\do\set_environment.do"

capture log close
log using "$log\2.5.construct_daa_for_analysis_`c(current_date)'.log" , replace
clear
set more off

foreach x in  first single { //
	foreach y in 2 5 10 30 50 { //
		use "$wdata\project_lottery_fertility_full_panel_93109.dta", clear

		merge m:1 win_year real_win_year pers_idn spouse_idn using "$wdata\single_and_first_event_winners.dta"
		drop if _m == 2
		drop _m
		
		keep if `x'_event_`y'k == 1
		keep if inrange(norm_year,-3,6) //from 3 years before to 6 year after lottery
		drop *event*k
		
		** Delete observation die before observation period
		drop if pers_death_year < win_year + 7

		** Prize Variable
		gen prize10M = winyr_pers_real_prize_aftertax / 10000 //prize in 10 Million
		gen prize5M = prize10M * 2

		** Interaction Terms
		gen currentxprize = 0
		replace currentxprize = prize5M if current == 1

		forv j = 3(-1)2{
			gen currentxprizexpre`j' = 0
			replace currentxprizexpre`j' = prize5M if current == 1 & norm_year==-`j'
		}
		forv j = 0(1)6{
			gen currentxprizexpost`j' = 0
			replace currentxprizexpost`j' = prize5M if current == 1 & norm_year==`j'
		}
		
		** Household variables
		foreach var in income earnings asset liquidasset{
			gen avg_`var' = pers_`var' if spouse_idn == ""
			replace avg_`var' = (pers_`var' + spouse_`var')/2 if spouse_idn ~= ""
		}
		
		** Variable at pre 1
		foreach var in totbaby married avg_income avg_earnings avg_asset avg_liquidasset{
			gen v1 = `var' if norm == -1
			egen `var'_pre1 = mean(v1), by(houseid) 
			drop v1
		}
		gen avg_labor_pre1 = avg_earnings_pre1 > 0
		
		** Variable at pre 1 (winner level)
		foreach var in pers_income pers_earnings pers_asset pers_liquidasset pers_saving{
			gen v1 = `var' if norm == -1
			egen `var'_pre1 = mean(v1), by(houseid) 
			drop v1
		}
		gen pers_labor_pre1 = pers_earnings_pre1 > 0
		
		** Lottery Participation
		merge 1:1 pers_idn year using "$wdata\count_prize_redeem.dta"
		drop if _m == 2
		drop _m
		
		foreach var in redeem_lotto redeem_receipt redeem_sport{
			recode `var' . =0
		}
		
		forv i = 1(1)3{
			foreach var in redeem_lotto redeem_receipt{
				gen v1 = `var' if norm == -`i' & current == 1
				replace v1 = `var' if norm == -`i' + 7 & future == 1
				egen pre`i'`var' = mean(v1), by(houseid)
				drop v1
			}
		}
		
		gen tot_redeem_lotto = pre1redeem_lotto + pre2redeem_lotto + pre3redeem_lotto
		gen tot_redeem_receipt = pre1redeem_receipt + pre2redeem_receipt + pre3redeem_receipt

		** Other variables
		gen agegroup = .
		replace agegroup = 2025 if win_age>=20 & win_age<25
		replace agegroup = 2530 if win_age>=25 & win_age<30
		replace agegroup = 3035 if win_age>=30 & win_age<35
		replace agegroup = 3540 if win_age>=35 & win_age<40
		replace agegroup = 4045 if win_age>=40 & win_age<45

		xtset houseid norm_year
		keep if win_year != 102

		dis "Sample: `x' Event, cut at `y'K"
		tab win_year future if norm == 0

		save "$wdata\project_lottery_fertility_b3a6_`x'_`y'k.dta", replace
	}
}



foreach x in  first { 
	foreach y in 5{ 
		use "$wdata\project_lottery_fertility_full_panel_93109.dta", clear

		merge m:1 win_year real_win_year pers_idn spouse_idn using "$wdata\single_and_first_event_winners.dta"
		drop if _m == 2
		drop _m
		
		keep if `x'_event_`y'k == 1
		
		replace win_year = real_win_year - 9 if future == 1
		replace norm = year - win_year
		
		keep if inrange(norm_year,-3,8) //from 3 years before to 8 year after lottery
		drop *event*k
		
		** Delete observation die before observation period
		drop if pers_death_year < win_year + 9

		** Prize Variable
		gen prize10M = winyr_pers_real_prize_aftertax / 10000 //prize in 10 Million
		gen prize5M = prize10M * 2
		
		** Interaction Terms
		gen currentxprize = 0
		replace currentxprize = prize5M if current == 1

		forv j = 3(-1)2{
			gen currentxprizexpre`j' = 0
			replace currentxprizexpre`j' = prize5M if current == 1 & norm_year==-`j'
		}
		forv j = 0(1)8{
			gen currentxprizexpost`j' = 0
			replace currentxprizexpost`j' = prize5M if current == 1 & norm_year==`j'
		}
		
		** Household variables
		foreach var in income earnings asset liquidasset{
			gen avg_`var' = pers_`var' if spouse_idn == ""
			replace avg_`var' = (pers_`var' + spouse_`var')/2 if spouse_idn ~= ""
		}
		
		** Variable at pre 1
		foreach var in totbaby married avg_income avg_earnings avg_asset avg_liquidasset{
			gen v1 = `var' if norm == -1
			egen `var'_pre1 = mean(v1), by(houseid) 
			drop v1
		}
		gen avg_labor_pre1 = avg_earnings_pre1 > 0
		
		** Variable at pre 1 (winner level)
		foreach var in pers_income pers_earnings pers_asset pers_liquidasset pers_saving{
			gen v1 = `var' if norm == -1
			egen `var'_pre1 = mean(v1), by(houseid) 
			drop v1
		}
		gen pers_labor_pre1 = pers_earnings_pre1 > 0
		
		** Lottery Participation
		merge 1:1 pers_idn year using "$wdata\count_prize_redeem.dta"
		drop if _m == 2
		drop _m
		
		foreach var in redeem_lotto redeem_receipt redeem_sport{
			recode `var' . =0
		}
		
		forv i = 1(1)3{
			foreach var in redeem_lotto redeem_receipt{
				gen v1 = `var' if norm == -`i' & current == 1
				replace v1 = `var' if norm == -`i' + 7 & future == 1
				egen pre`i'`var' = mean(v1), by(houseid)
				drop v1
			}
		}
		
		gen tot_redeem_lotto = pre1redeem_lotto + pre2redeem_lotto + pre3redeem_lotto
		gen tot_redeem_receipt = pre1redeem_receipt + pre2redeem_receipt + pre3redeem_receipt

		** Other variables
		gen agegroup = .
		replace agegroup = 2025 if win_age>=20 & win_age<25
		replace agegroup = 2530 if win_age>=25 & win_age<30
		replace agegroup = 3035 if win_age>=30 & win_age<35
		replace agegroup = 3540 if win_age>=35 & win_age<40
		replace agegroup = 4045 if win_age>=40 & win_age<45

		xtset houseid norm_year
		keep if inrange(win_year,96,100)

		dis "Sample: `x' Event, cut at `y'K"
		tab win_year future if norm == 0

		save "$wdata\project_lottery_fertility_b3a8_`x'_`y'k.dta", replace
	}
}

log close
